﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Reflection;

namespace aztall.Module.BusinessObjects
{
	[AttributeUsage(AttributeTargets.Class)]
	public class ImportClassAttribute : Attribute
	{
		public string ClassName { get; set; }
		public string KeyProperties { get; set; }

		public ImportClassAttribute([CallerMemberName] string className = "", string keyProperties = "")
		{
			ClassName = className;
			KeyProperties = keyProperties;
		}
	}
	[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
	public class ImportColumnAttribute : Attribute
	{
		public int ColumnPos { get; set; }    //column position in the import file, 1st column is one
		public string PropName { get; set; }
		public bool IsOptional { get; set; }
		public string DefaultIfMissing { get; set; }
		public ImportColumnAttribute(int columnIndex, bool isOptional = true, string defaultIfMissing = null, string keyProperties = "", [CallerMemberName] string propName = "")
		{
			ColumnPos = columnIndex;
			PropName = propName;
			IsOptional = isOptional;
			DefaultIfMissing = defaultIfMissing;
		}
	}
	public class Mapping<T> where T : DomainObject
	{
		public IList<ColumnMapping> _ColumnMappings = new List<ColumnMapping>();
		public Mapping()
		{
			Type t = typeof(T);
			var props = t.GetProperties(BindingFlags.Public | BindingFlags.Instance).Where(p => p.GetCustomAttribute<ImportColumnAttribute>() != null).ToList();
			props.ForEach(c => _ColumnMappings.Add(new ColumnMapping(c)));
		}
		public IList<ColumnMapping> ColumnMappings
		{
			get { return _ColumnMappings; }
			set { _ColumnMappings = value; }
		}

	}

	public class ColumnMapping
	{
		[DisplayName("Position")]
		public int ColumnPos { get; set; }    // first column is one
		public string PropName { get; set; }
		[DisplayName("Name")]
		public string PropDisplayName { get; set; }   // property DisplayName
		public bool IsOptional { get; set; }
		public string DefaultIfMissing { get; set; }  // token : {CurrentDate}, {Sequence}
		[DisplayName("Data Type")]
		public Type PropType { get; set; }
		public ColumnMapping(PropertyInfo prop)
		{
			ImportColumnAttribute attr = prop.GetCustomAttribute<ImportColumnAttribute>();
			System.ComponentModel.DisplayNameAttribute captionAttr1 = prop.GetCustomAttribute<System.ComponentModel.DisplayNameAttribute>();
			DevExpress.Xpo.DisplayNameAttribute captionAttr2 = prop.GetCustomAttribute<DevExpress.Xpo.DisplayNameAttribute>();
			DevExpress.ExpressApp.DC.XafDisplayNameAttribute captionAttr3 = prop.GetCustomAttribute<DevExpress.ExpressApp.DC.XafDisplayNameAttribute>();

			ColumnPos = attr.ColumnPos;
			PropName = attr.PropName;
			PropType = prop.PropertyType;
			PropDisplayName = (captionAttr3?.DisplayName ?? captionAttr1?.DisplayName ?? captionAttr2?.DisplayName ?? attr.PropName);
			DefaultIfMissing = attr.DefaultIfMissing ?? string.Empty;
			IsOptional = attr.IsOptional;

		}
	}
}
